<table>
    <thead>
        <tr>
            <th>STT</th>
            <th>Sản phẩm</th>
            <th>Mã</th>
            <th>SL</th>
            <th>Lựa chọn</th>
            <th>Thành tiền</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php
			include_once("service/productService.php");
			if(isset($_SESSION["cart"])){
				$cart = $_SESSION["cart"];
				$total = 0;
                $index = 0;
                
				foreach($cart as $each){
					$productInfo =  findProductBy($each[0]);
					$result = findImagesOfProduct($each[0]);
					$discountedPrice = getDiscountedAndOptionalPriceOfProduct($productInfo["PRO_ID"]);
									
					$total += $discountedPrice * $each[1];
					
					$firstImage = mysql_fetch_array($result);
					if($firstImage["IMG_PATH"] != "") $imagePath = "image/product/".$each[0]."/thumbnail/".$firstImage["IMG_PATH"];
					else $imagePath = "image/product/no-image.jpg";
                    
                    $optionalPrices = getOptionalPricesOfCategory($productInfo["CAT_ID"]);  
        ?>
        <tr>
            <td class="no w30"></td>
            <td>
            	<div class="product-thumbnail-wrapper">
                	<div class="thumbnail-product"><a href="product-detail.php?product=<?php echo $productInfo["PRO_ID"];?>"><img src="<?php echo $imagePath;?>" /></a></div>
                	<div class="product-name"><a href="product-detail.php?product=<?php echo $productInfo["PRO_ID"];?>"><?php echo $productInfo["PRO_NAME"];?></a></div>
               	</div>
            </td>
            <td><?php echo $productInfo["PRO_CODE"];?></td>
            <td><input type="text" id="<?php echo $index;?>" value="<?php echo $each[1];?>" class="w50" onkeyup="this.value = formatCurrency(this.value,2);"/></td>
            <td>
                    <?php
                        if(is_array($each[2])){
                            foreach(array_values($each[2]) as $optionItemId){
                                $optionalPrice = mysql_fetch_array(findOptionalPriceByOptionItem($optionItemId)); 
                                echo "<div>".$optionalPrice["OPP_NAME"].": ";
                                $optionItem = mysql_fetch_array(findOptionItemBy($optionItemId)); 
                                echo $optionItem["OOI_NAME"]."</div>";
                            }
                        }
                    ?>
            </td>
            <td class="money w100" ><?php echo number_format($discountedPrice * $each[1]);?></td>
            <td class="action w60">
            	<a href"#" onclick="updateCart(<?php echo $index;?>);"><img src="image/layout/update-btn-icon.png" /></a>
	            <a href="action/removeProductFromCartAction.php?id=<?php echo $index;?>"><img src="image/layout/delete-btn-icon.png"/></a>
            </td>
        </tr>
        <?php
                    $index++;
				}
			}
        ?>
    </tbody>
</table>
<script>
	function updateCart(proId){
		var url = 'action/updateProductFromCartAction.php?proId=' + proId + '&quantity=' + $("#"+proId).val();
		submitAction(url);
	}
</script>
<?php
	if(isset($_SESSION["cart"]) && count($_SESSION["cart"]) != 0){
?>
<div class="total-wrapper">
    <div>Tổng cộng</div>
    <div><?php echo number_format($total);?></div>
</div>
<div class="clear"></div>
<div class="checkout-wrapper">
    <div onclick="nextStep();">Mua hàng</div>
    <div class="clear"></div>
    <div><a href="action/removeCartAction.php">Hủy bỏ & tiếp tục mua hàng</a></div>	   
</div>
<?php
	}
?>
<script>
	$("#deliveryTab").css("display", "none");
	
	function nextStep(){
		$("#deliveryTab").css("display", "inline");
		submitAction('profile-cart.php#deliveryInfo-tab');
		$("#deliveryTab").addClass("active");
	}
</script>